var mongoose = require('mongoose')

var OrderSchema = new mongoose.Schema({
	uId: {type: String},
	ordername: { type: String },
	uName: {type: String},
	location: { type: String },
	tel:{ type: Number },
  //0: 订单正在进行
  //1：已完成订单
  //2：订单取消
	cStatus: { type: Number, default: 0 },
  pay:{ type: Boolean, default: false  },
	cart:[],
   	total: {type: Number, default: 0},
	meta: {
		createAt: {
		  type: Date,
		  default: Date.now()
		}
	}
})

OrderSchema.pre('save', function(next) {
  var Cart = this

  if (this.isNew) {
    this.meta.createAt = this.meta.updateAt = Date.now()
  }
  else {
    this.meta.updateAt = Date.now()
  }
  next()
})

OrderSchema.statics = {
  fetch: function(cb) {
    return this
      .find({})
      .sort('meta.updateAt')
      .exec(cb)
  },
  findById: function(id, cb) {
    return this
      .findOne({_id: id})
      .exec(cb)
  }
}

module.exports = OrderSchema
